package com.team.arms.retire.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.bingyiInfo.entity.BingyiInfo;
import com.team.arms.bingyiInfo.entity.vo.BingyiInfoVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.retire.domain.RetireRegister;
import com.team.arms.retire.domain.dto.AdvanceDTO;
import com.team.arms.retire.service.RetireRegisterService;
import com.team.arms.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: WY
 * @date: 2022/6/1
 */

@RestController
@RequestMapping("/retire")
@CrossOrigin
@Api(tags="退伍军人模块")
public class RegisterController extends BaseController {
    @Autowired
    private RetireRegisterService retireRegisterService;

    @GetMapping("/page/getlist/{pageNum}/{size}")
    @ApiOperation("分页展示退伍信息")
    public AjaxResult pageInfo(@PathVariable Integer pageNum, @PathVariable Integer size){
        List<RetireRegister> list = retireRegisterService.list();
        Page page = PageUtil.list2Page(list, pageNum, size);
        return page != null ? AjaxResult.success("分页成功",page):AjaxResult.error("分页失败");
    }

    @PostMapping("/add")
    @ApiOperation("新增退伍信息")
    public AjaxResult getRetireRegister(@RequestBody RetireRegister retireRegister){
        return toAjax(retireRegisterService.save(retireRegister));
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("删除单个退伍信息")
    public AjaxResult removeById(@PathVariable String id){
        return toAjax(retireRegisterService.removeById(id));
    }

    @DeleteMapping("/removeByIds")
    @ApiOperation("批量删除退伍军人信息")
    public AjaxResult removeByIds(@RequestBody String[] ids){
        ArrayList<String> list = new ArrayList<String>();
        for(String id : ids){
            list.add(id);
        }
        return toAjax(retireRegisterService.removeBatchByIds(list));
    }

    @PutMapping("/update")
    @ApiOperation("修改退伍军人信息")
    public AjaxResult updateRetireRegister(@RequestBody RetireRegister retireRegister){
        return toAjax(retireRegisterService.updateById(retireRegister));
    }

    @PostMapping("/select/name")
    @ApiOperation("根据姓名查询退伍信息")
    public AjaxResult selectByName(@RequestParam String name){
        QueryWrapper query = new QueryWrapper();
        query.like("name",name);
        List<RetireRegister> listRetireRegister = retireRegisterService.list(query);
        return AjaxResult.success(listRetireRegister);
    }

    @ApiOperation("单个条件查询退伍信息")
    @PostMapping("/select/condition")
    public AjaxResult selectByCodition(@RequestParam String condition,@RequestParam String content){
        QueryWrapper<RetireRegister> query=new QueryWrapper();
        if (condition.equals("姓名")){
            query.like("name",content);
        }else if (condition.equals("性别")){
            query.like("sex",content);
        }else if (condition.equals("报名入伍乡镇")){
            query.like("town",content);
        }else if (condition.equals("政治面貌")){
            query.like("poli_status",content);
        }else if(condition.equals("身份证号")){
            query.like("id_card",content);
        }else if(condition.equals("学历")){
            query.like("education",content);
        }

        List<RetireRegister> list = retireRegisterService.list(query);
        return AjaxResult.success(list);
    }

    @ApiOperation("高级查询退伍军人信息")
    @PostMapping("/select/advance")
    public AjaxResult selectAdvance(@RequestBody AdvanceDTO advanceDTO){
        String name=advanceDTO.getName();
        String sex=advanceDTO.getSex();
        String town=advanceDTO.getTown();
        String poliStatus=advanceDTO.getPoliStatus();
        String idCard=advanceDTO.getIdCard();
        String education=advanceDTO.getEducation();
        //要实现的是查询两个字段
        //只and查询这两个字段的
        //其他字段则不管 允许为null
        QueryWrapper<RetireRegister> query=new QueryWrapper();
        if(name!=null){
            query.like("name",name);
        }

        if(sex!=null){
            query.like("sex",sex);
        }

        if (town!=null){
            query.like("town",town);
        }

        if(poliStatus!=null){
            query.like("poli_status",poliStatus);
        }

        if(idCard!=null){
            query.like("id_card",idCard);
        }

        if (education!=null){
            query.like("education",education);
        }

        List<RetireRegister> list= retireRegisterService.list(query);
        return AjaxResult.success(list);
    }

    @ApiOperation(("获取详细的退伍军人信息"))
    @GetMapping("/getInfo/{id}")
    public AjaxResult getRetireRegister(@PathVariable String id){
        return AjaxResult.success(retireRegisterService.getById(id));
    }

    @ApiOperation("导出退伍军人列表")
    @Log(title = "退伍信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库

        List<RetireRegister> retireRegisterlist = retireRegisterService.list();
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), RetireRegister.class).sheet("退伍军人信息数据")
                    .doWrite(retireRegisterlist);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
